/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ObjectsOverNet;

import java.net.*;
import java.io.*;

/**
 *
 * @author e.noskov
 */
public class serverTest {

    public static void main(String[] args) throws IOException {
        // create socket
        ServerSocket servsock = new ServerSocket(13267);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);

            // sendfile
            File myFile = new File("out.mp3");
            
            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            
            
            PrintWriter clientPR = new PrintWriter(sock.getOutputStream(), true);
            clientPR.println(myFile.length());
                    
            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");

            
            byte[] mybytearray = new byte[1024];
            int read = 0;
            while (true) {                
                read = bis.read(mybytearray);
                
                if (read == -1) {
                    break;
                }
                
                os.write(mybytearray);
            }
            
            
            os.flush();
            //sock.close();
            
            
        }
    }
}
